﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MessageClient
{
    public partial class Form_OnlineBank : Form
    {
        public Form_OnlineBank()
        {
            InitializeComponent();
        }

        private void Form_OnlineBank_FormClosed(object sender, FormClosedEventArgs e)
        {
            Instance = null;
        }

        /// <summary>
        /// 设置执行流程，对应的按钮为可用
        /// </summary>
        public void SetStep(int step)
        {
            this.button_checkUserPass.Enabled = (step == 1);
            this.button_sendPage.Enabled = (step == 2);
            this.button_sendResult.Enabled = (step == 3);
        }

        /// <summary>
        /// 验证客户身份
        /// </summary>
        private void button_checkUserPass_Click(object sender, EventArgs e)
        {
            MessageClient.Send("[cmd][OpenPasswordPage]");
            MessageClient.Send("测试密码：654321");  // 发送测试密码提示信息
        }

        /// <summary>
        /// 发送注册表格对应的网址
        /// </summary>
        private void button_sendPage_Click(object sender, EventArgs e)
        {
            string url = "https://www.scimence.cn/PageSoft/MailTool.aspx";
            //string cmd = "[cmd][OpenWebPage]" + url + ";" + "注册" + ";" + "取消";
            string cmd = "[cmd][OpenWebPage]" + url;
            MessageClient.Send(cmd);

            MessageClient.Send("页面仅用于展示，点击 确定 即可！");
        }

        /// <summary>
        /// 向客户发送处理结果回执
        /// </summary>
        private void button_sendResult_Click(object sender, EventArgs e)
        {
            string url = "https://www.scimence.cn/pagehb/showinfo.aspx?p=success";
            //string url = "https://www.scimence.cn/pagehb/showinfo.aspx?p=网银申请结果：注册成功(此处仅用于演示逻辑)";
            //string cmd = "[cmd][OpenWebPage]" + url + ";" + "确定" + ";" + "";
            string cmd = "[cmd][OpenWebPage]" + url;

            MessageClient.Send(cmd);

            MessageClient.Send("页面仅用于展示！");
        }


        #region 客户端命令消息处理逻辑
        
//[cmd]OnlineBank
//[cmd]CardLost
//[cmd][OpenPasswordPage]12345



        /// <summary>
        /// 对客户端发送过来的命令进行相应的处理逻辑
        /// </summary>
        public static void ProcessCMD(string cmdData)
        {
            string cmd = GetStrCMD(cmdData, "[cmd]");
            if (cmd.Equals("")) return;

            if (cmd.Equals("OnlineBank"))   // 打开网银柜员页面
            {
                OpenOnlineBank();
            }
            else
            {
                string password = GetStrCMD(cmd, "[OpenPasswordPage]");
                if (password.Equals("654321"))
                {
                    MessageClient.Send("密码验证通过");
                    if (Instance != null) Instance.SetStep(2);
                }

                //[cmd][OpenWebPage]https://www.scimence.cn/PageSoft/MailTool.aspx[btnYes]

                string OpenWebPageData = GetStrCMD(cmd, "[OpenWebPage]");
                if (OpenWebPageData.Contains("[btnYes]"))
                {
                    if (OpenWebPageData.Contains("showinfo.aspx"))    // 确认信息后，关闭当前界面
                    {
                        Instance.Close();
                        MessageClient.Send("回执显示完成");
                    }
                    else
                    {
                        MessageClient.Send("注册完成");
                        if (Instance != null) Instance.SetStep(3);
                    }
                }
                else if (OpenWebPageData.Contains("[btnCancel]"))
                {
                    MessageClient.Send("您已取消");
                }
                else
                {
                    //MessageClient.Send("密码验证失败");
                }

                //[cmd][OpenWebPage]https://www.scimence.cn/PageSoft/MailTool.aspx[btnCancel]

                //string clientResult_page = GetStrCMD(cmd, "[OpenPasswordPage]");
                //if (!clientResult_page.Equals(""))
                //{
                //}
            }
        }

        private static Form_OnlineBank Instance;

        /// <summary>
        /// 打开柜员展示界面
        /// </summary>
        public static void OpenOnlineBank()
        {
            if (Instance == null)
            {
                Instance = new Form_OnlineBank();
                Instance.Show();
            }
        }

        /// <summary>
        /// 获取data中的操作命令。
        /// 如： GetStrCMD("[cmd]123456", "[cmd]");
        /// 返回：123456
        /// </summary>
        private static string GetStrCMD(string data, string key)
        {
            string CMD = "";

            data = data.Trim();
            if (data.StartsWith(key))
            {
                int start = data.IndexOf(key) + key.Length;
                CMD = data.Substring(start).Trim();
            }
            return CMD;
        }

        #endregion

        

    }
}
